package com.the_ring.chapter06.databse;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.the_ring.chapter06.bean.GoodsInfo;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DB_MAME = "shopping";
    private static final String GOODS_TABLE = "goods";
    private static final String CART_TABLE = "cart";

    private static DatabaseHelper databaseHelper;
    private SQLiteDatabase database;

    public static DatabaseHelper getInstance(Context context) {
        if (databaseHelper == null) {
            databaseHelper = new DatabaseHelper(context, DB_MAME, null, 1);
        }
        return databaseHelper;
    }

    public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String goodsTable = "create table if not exists " + GOODS_TABLE + " (" +
                "rowId integer Primary key AUTOINCREMENT," +
                "name varchar," +
                "detail text," +
                "pic_path varchar," +
                "price float);";
        db.execSQL(goodsTable);

        String cartTable = "create table if not exists " + CART_TABLE + " (" +
                "rowId integer)";
        db.execSQL(cartTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void openWriteLink() {
        if (database == null || !database.isOpen()) {
            database = getWritableDatabase();
        }
    }

    public void closeLink() {
        if (database != null && database.isOpen()) {
            database.close();
        }
        database = null;
    }

    /**
     * 获取所有商品
     * @return
     */
    public List<GoodsInfo> queryAllGoods() {
        String sql = "select rowId, name, detail, pic_path, price from " + GOODS_TABLE;
        List<GoodsInfo> infoList = new ArrayList<>();
        Cursor cursor = database.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            GoodsInfo info = new GoodsInfo();
            info.rowId = cursor.getLong(0);
            info.name = cursor.getString(1);
            info.detail = cursor.getString(2);
            info.picPath = cursor.getString(3);
            info.price = cursor.getFloat(4);
            infoList.add(info);
        }
        cursor.close();
        return infoList;
    }

    public GoodsInfo queryById(long rowId) {
        Cursor cursor = database.query(GOODS_TABLE, new String[]{"rowId", "name", "detail", "pic_path", "price"}, "rowId=" + rowId, null, null, null, null);
        GoodsInfo info = new GoodsInfo();
        if (cursor.moveToNext()) {
            info.rowId = cursor.getLong(0);
            info.name = cursor.getString(1);
            info.detail = cursor.getString(2);
            info.picPath = cursor.getString(3);
            info.price = cursor.getFloat(4);
        }
        cursor.close();
        return info;
    }

    /**
     * 购物车的数量
     * @return
     */
    public int countOfCart() {
        String sql = "select * from " + CART_TABLE + ";";
        Cursor cursor = database.rawQuery(sql, null);
        int count = 0;
        if (cursor.moveToNext()) {
            count = cursor.getCount();
            Log.i("Helper", "size: " + cursor.getColumnCount());
        }
        cursor.close();
        return count;
    }

    public List<GoodsInfo> queryCarList() {
        List<GoodsInfo> infoList = new ArrayList<>();
        Cursor cursor = database.query(CART_TABLE, new String[]{"rowId", "count(*)"}, null, null, "rowId", null, null);
        while (cursor.moveToNext()) {
            GoodsInfo info = new GoodsInfo();
            info.rowId = cursor.getLong(0);
            info.num = cursor.getInt(1);
            infoList.add(info);
        }
        cursor.close();

        for (GoodsInfo info : infoList) {
            GoodsInfo tmp = queryById(info.rowId);
            info.name = tmp.name;
            info.detail = tmp.detail;
            info.picPath = tmp.picPath;
            info.price = tmp.price;
        }
        return infoList;
    }

    public void insertToCart(long rowId) {
        ContentValues val = new ContentValues();
        val.put("rowId", rowId);
        database.insert(CART_TABLE, null, val);
    }

    public void clearCart() {
        database.delete(CART_TABLE, null, null);
    }

    /**
     * 插入数据到商品表
     * @param info
     * @return
     */
    public long insert(GoodsInfo info) {
        ContentValues val = new ContentValues();
        val.put("name", info.name);
        val.put("detail", info.detail);
        return database.insert(GOODS_TABLE, null, val);
    }

    /**
     * 更新数据
     * @param info
     */
    public void  update(GoodsInfo info) {
        ContentValues val = new ContentValues();
        val.put("name", info.name);
        val.put("detail", info.detail);
        val.put("pic_path", info.picPath);
        val.put("price", info.price);
        database.update(GOODS_TABLE, val, String.format("rowId=%s", String.valueOf(info.rowId)), null);
    }
}
